﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.CompilerServices;


[assembly: InternalsVisibleTo("DAL")]
namespace DAL.Gateway
{
    internal class DataAccessGateway
    {
        public static void CerrarConexion()
        {
            Gateway.Conexion.GDatos.CerrarConexion();
        }
        private static string InicializarConexion(string guid)
        {
            try
            {
                Gateway.Conn conn = null;

                using (Gateway.BaseDA conector = new Gateway.BaseDA())
                {
                    conn = conector.GetConn(guid);
                }

                if (conn == null)
                {
                    throw new Exception("[No se encuentra el SP o no está activo.]");
                }

                Gateway.Conexion.IniciarSesion(conn.ConnectionString, conn.DBType);

                return conn.SPName;
            }
            catch (Exception ex)
            {
                throw new Exception("DataAccessGateway - InicializarConexion - ", ex);
            }

        }
        public static int Ejecutar(string guid)
        {
            try
            {
                int result = 0;
                string spName = InicializarConexion(guid);
                result = Gateway.Conexion.GDatos.Ejecutar(spName);
                return result;
            }
            catch (Exception ex)
            {
                throw new Exception("DataAccessGateway - Ejecutar - ", ex);
            }
            finally
            {
                Gateway.Conexion.GDatos.CerrarConexion();
            }
        }

        public static int Ejecutar(string guid, params object[] args)
        {
            try
            {
                int result = 0;
                string spName = InicializarConexion(guid);
                result = Gateway.Conexion.GDatos.Ejecutar(spName,args);
                return result;
            }
            catch (Exception ex)
            {
                throw new Exception("DataAccessGateway - Ejecutar - ", ex);
            }
            finally
            {
                Gateway.Conexion.GDatos.CerrarConexion();
            }
        }
        public static System.Data.IDataReader TraerDataReader(string guid, params object[] args)
        {
            try
            {
                System.Data.IDataReader dr;
                string spName = InicializarConexion(guid);
                dr = Gateway.Conexion.GDatos.TraerDataReader(spName,args);
                return dr;
            }
            catch (Exception ex)
            {
                throw new Exception("DataAccessGateway - Ejecutar - ", ex);
            }
            finally
            {
                Gateway.Conexion.GDatos.CerrarConexion();
            }
        }

        public static System.Data.IDataReader TraerDataReader(string guid)
        {
            try
            {
                System.Data.IDataReader dr;
                string spName = InicializarConexion(guid);
                dr = Gateway.Conexion.GDatos.TraerDataReader(spName);
                return dr;
            }
            catch (Exception ex)
            {
                throw new Exception("DataAccessGateway - Ejecutar - ", ex);
            }
            finally
            {
                Gateway.Conexion.GDatos.CerrarConexion();
            }
        }

        public static System.Data.DataSet TraerDataSet(string guid)
        {
            try
            {
                System.Data.DataSet dt;
                string spName = InicializarConexion(guid);
                dt = Gateway.Conexion.GDatos.TraerDataSet(spName);
                return dt;
            }
            catch (Exception ex)
            {
                throw new Exception("DataAccessGateway - Ejecutar - ", ex);
            }
            finally
            {
                Gateway.Conexion.GDatos.CerrarConexion();
            }
        }

        public static System.Data.DataSet TraerDataSet(string guid, params object[] args)
        {
            try
            {
                System.Data.DataSet dt;
                string spName = InicializarConexion(guid);
                dt = Gateway.Conexion.GDatos.TraerDataSet(spName,args);
                return dt;
            }
            catch (Exception ex)
            {
                throw new Exception("DataAccessGateway - Ejecutar - ", ex);
            }
            finally
            {
                Gateway.Conexion.GDatos.CerrarConexion();
            }
        }

        public static System.Data.DataTable TraerDataTable(string guid)
        {
            try
            {
                System.Data.DataTable dt;
                string spName = InicializarConexion(guid);
                dt = Gateway.Conexion.GDatos.TraerDataTable(spName);
                return dt;
            }
            catch (Exception ex)
            {
                throw new Exception("DataAccessGateway - Ejecutar - ", ex);
            }
            finally
            {
                Gateway.Conexion.GDatos.CerrarConexion();
            }
        }

        public static System.Data.DataTable TraerDataTable(string guid, params object[] args)
        {
            try
            {
                System.Data.DataTable dt;
                string spName = InicializarConexion(guid);
                dt = Gateway.Conexion.GDatos.TraerDataTable(spName,args);
                return dt;
            }
            catch (Exception ex)
            {
                throw new Exception("DataAccessGateway - Ejecutar - ", ex);
            }
            finally
            {
                Gateway.Conexion.GDatos.CerrarConexion();
            }
        }

        public static object TraerValorEscalar(string guid)
        {
            try
            {
                object result = null;
                string spName = InicializarConexion(guid);
                result = Gateway.Conexion.GDatos.TraerValorEscalar(spName);
                return result;
            }
            catch (Exception ex)
            {
                throw new Exception("DataAccessGateway - Ejecutar - ", ex);
            }
            finally
            {
                Gateway.Conexion.GDatos.CerrarConexion();
            }
        }

        public static object TraerValorEscalar(string guid, params object[] args)
        {
            try
            {
                object result = null;
                string spName = InicializarConexion(guid);
                result = Gateway.Conexion.GDatos.TraerValorEscalar(spName,args);
                return result;
            }
            catch (Exception ex)
            {
                throw new Exception("DataAccessGateway - Ejecutar - ", ex);
            }
            finally
            {
                Gateway.Conexion.GDatos.CerrarConexion();
            }
        }
        public static object TraerValorOutput(string guid)
        {
            try
            {
                object result = null;
                string spName = InicializarConexion(guid);
                result = Gateway.Conexion.GDatos.TraerValorOutput(spName);
                return result;
            }
            catch (Exception ex)
            {
                throw new Exception("DataAccessGateway - Ejecutar - ", ex);
            }
            finally
            {
                Gateway.Conexion.GDatos.CerrarConexion();
            }
        }

        public static object TraerValorOutput(string guid, params object[] args)
        {
            try
            {
                object result = null;
                string spName = InicializarConexion(guid);
                result = Gateway.Conexion.GDatos.TraerValorOutput(spName,args);
                return result;
            }
            catch (Exception ex)
            {
                throw new Exception("DataAccessGateway - Ejecutar - ", ex);
            }
            finally
            {
                Gateway.Conexion.GDatos.CerrarConexion();
            }
        }
    }
}
